package cn.com.template.controller.project;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jxls.transformer.XLSTransformer;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.commons.Paginator;
import cn.com.commons.utils.CommonUtils;
import cn.com.commons.utils.StringUtil;
import cn.com.dal.dataobject.cs.NewProjectDO;
import cn.com.dal.dataobject.cs.NewProjectDetailDO;
import cn.com.dal.dataobject.cs.ProjectDO;
import cn.com.dal.dataobject.cs.TotalReportEntity;
import cn.com.dal.dataobject.hr.DeptDO;
import cn.com.dal.dataobject.sys.DictDO;
import cn.com.service.cs.NewProjectDetailService;
import cn.com.service.cs.NewProjectService;
import cn.com.service.cs.ProjectService;
import cn.com.service.hr.DeptService;
import cn.com.service.sys.DictService;
import cn.com.support.context.UserContext;
import cn.com.support.message.MessageCodeEnum;
import cn.com.support.message.MessageResult;
import cn.com.support.utils.JacksonUtil;
import cn.com.support.utils.TemplateUtils;
import cn.com.template.ui.Flexigrid;
import cn.com.template.utils.FlexigridUtil;

/**
 * 
 * 公司项目管理控制器[修改版]
 * 
 * @since 1.0 2012-8-31 下午11:03:53
 * @version 1.0
 * @author Zoe
 */
@Controller
@RequestMapping("/project/newprosummary")
public class NewProjectSummaryController {

	@Autowired
	private NewProjectDetailService newproDetailService;
	/**
	 * 跳转页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping
	public ModelAndView forward(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/project/newprosummary");
		return mav;
	}

	/**
	 * 分页查询
	 * 
	 * @param paginator
	 * @return
	 */
	@RequestMapping("/selectForPage.json")
	@ResponseBody
	public Flexigrid selectForPage(HttpServletRequest request,
			HttpServletResponse response) {
		// 封装参数
		Paginator<Object, List<NewProjectDetailDO>> paginator = new Paginator<Object, List<NewProjectDetailDO>>();
		Integer page = TemplateUtils.getParam(Integer.class, request, "page", 1);
		Map<Object, Object> condition = new HashMap<Object, Object>();
		String pname = request.getParameter("name");
		String deptName = request.getParameter("deptName");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String type = request.getParameter("type");
		if("11".equals(type)){
			type = "1";
			condition.put("gmtRefund","");
		}else if("22".equals(type)){
			type = "2";
			condition.put("gmtRefund","");
		}
		condition.put("type", type);
		condition.put("name", pname);
		condition.put("deptName", deptName);
		if(!("").equals(startTime)){
			condition.put("startTime", startTime);
		}
		if(!("").equals(endTime)){
			condition.put("endTime", endTime);
		}
		// 封装查询时间
		paginator.setCurrentPage(page);
		paginator.setCondition(condition);
		// 分页查询
		newproDetailService.selectByPage(paginator);
		// 返回结果
		return FlexigridUtil.formatFlexigridData(paginator);
	}
	
	@RequestMapping("/exportSummary/{name}/{dept}/{type}/{st}/{et}")
	public void exportSummary(HttpServletRequest request,
			HttpServletResponse response, @PathVariable("name") String name,@PathVariable("dept") String dept,
			@PathVariable("type") String type,
			@PathVariable("st") String st, @PathVariable("et") String et) throws UnsupportedEncodingException {
		// 报表数据源
		Map<String, Object> reportDataMap = new HashMap<String, Object>();
		// 表头
		TotalReportEntity reportEntity = new TotalReportEntity();
		// 封装参数
		Map<Object, Object> condition = new HashMap<Object, Object>();
		name = java.net.URLDecoder.decode(name, "UTF-8");
		if (StringUtil.isNotBlank(name) && !name.equals("-1")) {
			condition.put("name", name.trim());
		}
		if (StringUtil.isNotBlank(dept) && !dept.equals("-1")) {
			condition.put("dept", dept.trim());
		}
		if (StringUtil.isNotBlank(st) && !st.equals("-1")) {
			condition.put("startTime", st.trim());
			reportEntity.setDateFrom(st);
		}
		if (StringUtil.isNotBlank(et) && !et.equals("-1")) {
			condition.put("endTime", st.trim());
			reportEntity.setDateTo(et);
		}
		if (StringUtil.isNotBlank(type) && !type.equals("-1")) {
			if("11".equals(type)){
				type = "1";
				condition.put("gmtRefund","");
			}
			if("22".equals(type)){
				type = "2";
				condition.put("gmtRefund","");
			}
			condition.put("type", type.trim());
		}
		reportEntity.setYear(CommonUtils.getCurrentYear());
		reportEntity.setProjectReportLst(newproDetailService.selectSummaryList(condition));
		reportDataMap.put("totalForms", reportEntity);
		// 定义报表生成
		XLSTransformer transformer = new XLSTransformer();
		String path = request.getSession().getServletContext()
				.getRealPath("/WEB-INF/classes");
		String templateFileName = path + "/xlstemp/project_report.xls";
		try {
			InputStream is = new BufferedInputStream(new FileInputStream(
					templateFileName));
			// Workbook resultWorkbook = transformer.transformXLS(is,
			// reportDataMap);
			response.setHeader("Content-Transfer-Encoding", "base64");
			response.setContentType("application/octet-stream");
			String contentDisposition = "attachment;filename=\"project_report.xls\"";
			response.setHeader("Content-Disposition", contentDisposition);
			OutputStream outputStream = response.getOutputStream();
			transformer.transformXLS(is, reportDataMap).write(outputStream);
			outputStream.flush();
			outputStream.close();
			outputStream = null;
			transformer = null;
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
